﻿using DAL;
using GEMINIMainOP;
using Model;
using S7.Net;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Media;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Threading;
using System.Windows.Forms;
namespace MainOP
{
    public partial class FormMain : Form
    {
        string str = "未连接";
        string strs = "";
        int PLC_Count = 1;
        public static string historyTypeName;
        public static double[] OP10 = new double[5];
        public static double[] OP10_2 = new double[5];
        public static double[] OP20 = new double[5];
        public static double[] OP30 = new double[5];
        public static double[] OP40 = new double[5];
        public static double[] OP50 = new double[5];
        public static double[] OP60 = new double[5];
        public static double[] OP70 = new double[5];
        public static double[] OP80 = new double[5];
        public static double[] OP90 = new double[5];
        public static double[] OP100 = new double[5];
        public static double[] OP110 = new double[5];
        public static double[] OP120 = new double[5];
        public static double[] YCok = new double[5];
        public static double[] TimeS = new double[25];      //用于记录各个时间段的生产数量
        public static string[] TimeRange = new string[15];      //用于记录白班与夜班的时间段
        public static double[] TimeRangeValue = new double[15]; //用于记录白班与夜班的生产数量
        //public static bool Clear;   //用于调试赋值
        public static bool Clear;   //用于早8晚8对生产数量清一次0
        public struct proData
        {
            public static string typeName;
            public static string Ewm;
            public static string testStation;
            public static string allResult;
            public static double positivePressureValue;
            public static double negativePressureValue;
            public static string operatorter;
            public static string serialNumber;
            public static bool save;
        }
        public struct plcStructIn
        {
            public bool bool_0;
            public bool bool_1;
            public bool bool_2;
            public bool bool_3;
            public bool bool_4;
            public bool bool_5;
            public bool bool_6;
            public bool bool_7;
            public bool bool_8;
            public bool bool_9;
            public bool bool_10;
            public bool bool_11;
            public bool bool_12;
            public bool bool_13;
            public bool bool_14;
            public bool bool_15;
            public bool bool_16;
            public bool bool_17;
            public bool bool_18;
            public bool bool_19;
            public bool bool_20;
            public bool bool_21;
            public bool bool_22;
            public bool bool_23;

        }
        //----线程循环
        bool _Continue = true;
        //----PLC监控线程数组
        Thread[] PLC_Td;
        //PLC实例数组
        public SimenesPLC[] PLC_Link;
        //PLC DB结构数组
        public plcStructIn[] Struct_Test;
        public static plcStructIn _plcStruct = new plcStructIn();
        public static FormMain FMOP;

        public FormMain()
        {
            InitializeComponent();
            FMOP = (FormMain)this;
        }

        /// <summary>
        /// 打开窗体，将页面嵌入到主窗体
        /// </summary>
        /// <param name="form"></param>
        private void OpenForm(Form form)
        {
            //将当前子窗体设置成非顶级控件
            form.TopLevel = false;
            //设置窗体最大化
            form.WindowState = FormWindowState.Maximized;
            //设置窗体无边框
            form.FormBorderStyle = FormBorderStyle.None;
            //设置窗体嵌入容器
            form.Parent = this.panel1;
            form.Show();
        }
        /// <summary>
        /// 关闭窗体，将容器内窗体移除
        /// </summary>
        private void CloseForm()
        {
            //遍历这个容器里面的所有控件，找出窗体对象
            foreach (Control item in this.panel1.Controls)
            {
                if (item is Form)
                {
                    Form form = (Form)item;
                    form.Close();
                    //移除
                    this.panel1.Controls.Remove(item);
                }
            }
        }

        private void 用户管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //权限验证
            PernChk(Program.userlogin);
            if (Program.userlogin.UserID == 1)
            {
                //嵌入窗口
                this.CloseForm();
                FormUserManagement formUserManagement = new FormUserManagement();
                this.OpenForm(formUserManagement);
            }
            else
            {
                MessageBox.Show("你没有权限！");

            }
        }
        private void 用户注销ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Program.userlogin.Admin = null;
            this.CloseForm();
        }
        private void 参数设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //嵌入窗口
            this.CloseForm();
            FormParameterSetting formParameterSetting = new FormParameterSetting();
            this.OpenForm(formParameterSetting);
        }
        private void 排产设置ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.CloseForm();
            FormProductionScheduling formProductionScheduling = new FormProductionScheduling();
            this.OpenForm(formProductionScheduling);
        }
        private void pLC通讯ToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            this.CloseForm();
            FormPLC formPLC = new FormPLC();
            this.OpenForm(formPLC);
            FormPLC.textBox.Text = strs;
        }


        private void FormMain_Load(object sender, EventArgs e)
        {
            //取消跨线程的访问
            Control.CheckForIllegalCrossThreadCalls = false;
            #region 连接plc
            //---- PLC线程
            PLC_Td = new Thread[PLC_Count];
            //plc实例
            PLC_Link = new SimenesPLC[PLC_Count];
            //db结构
            Struct_Test = new plcStructIn[PLC_Count];
            //循环创建线程，并用线程执行委托函数
            for (int I = 0; I < PLC_Count; I++)
            {
                PLC_Td[I] = new Thread(PLC_Td_Func);
                PLC_Td[I].IsBackground = true;
                PLC_Td[I].Start(I);
            }
            #endregion
            #region 创建服务器
            try
            {
                //当点击开始监听的时候 在服务器端创建一个负责监IP地址跟端口号的Socket
                Socket socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                IPAddress ip = IPAddress.Any;//IPAddress.Parse(txtServer.Text);
                //创建端口号对象
                IPEndPoint point = new IPEndPoint(ip, 50001);
                //监听
                socketWatch.Bind(point);
                //ShowMsg("监听成功");
                socketWatch.Listen(20);

                Thread th = new Thread(Listen);
                th.IsBackground = true;
                th.Start(socketWatch);
            }
            catch
            { }

            #endregion
            label2.Text = "当前登录用户：{0} \t\t  使用公司：空调国际(上海)有限公司   \t 设备公司：重庆英之拓智能设备有限公司  \t  软件版本号：V2.0";
            label2.Text = string.Format(label2.Text, Program.userlogin.name);
            
        }

        int callsys = 0;
        bool callover;

        SoundPlayer sound = new SoundPlayer("op10呼叫班长.wav");
        //线程
        private void PLC_Td_Func(object ob)
        {
            Ping p = new Ping();
            int num = (int)ob;

            string[] PLC_IP = new string[PLC_Count];
            PLC_IP[0] = "172.16.1.2";
            //PLC_IP[1] = "192.168.0.160";

            PLC_Link[num] = new SimenesPLC(CpuType.S71200, PLC_IP[num].ToString(), 0, 1);

            while (_Continue)
            {
                try
                {
                    //处理相应的PLC对象的数据处理
                    if (p.Send(PLC_IP[num], 200).Status == IPStatus.Success)//如果200ms时间间隔ping通了
                    {
                        if (PLC_Link[num].plc.IsConnected == false)//plc没有连接上
                        {
                            //int s = (int)PLC_Link[num].plc.Open();
                            //strs = Convert.ToString(s);
                            //if (s == 0)
                            //{
                            //    str = "成功";
                            //}
                            //else
                            //{
                            //    str = "失败";
                            //}
                            lock (this)
                            {
                                try
                                {
                                    PLC_Link[0].plc.Open();
                                    SimenesPLC.conectStatus = true;
                                    str = "成功";
                                }
                                catch (Exception)
                                {
                                    SimenesPLC.conectStatus = false;
                                    str = "失败";
                                    throw;
                                }
                            }
                        }
                        else
                        {
                            str = "成功";
                            //读取DB
                            Struct_Test[num] = (plcStructIn)PLC_Link[num].plc.ReadStruct(typeof(plcStructIn), 99, 616);
                            _plcStruct = Struct_Test[num];
                            #region 合格率
                            OP10[0] = Math.Round(PLC_Link[num].readReal("DB99.DBD718.0") * 100, 2);
                            //OP10_2[0] = Math.Round(PLC_Link[num].readReal("DB99.DBD710.0") * 100, 2);
                            OP20[0] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD722.0") * 100, 2);*/ Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 722, VarType.Real, 1).ToString()) * 100, 2);
                            OP30[0] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD726.0") * 100, 2);*/ Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 726, VarType.Real, 1).ToString()) * 100, 2);
                            OP40[0] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD730.0") * 100, 2);*/ Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 730, VarType.Real, 1).ToString()) * 100, 2);
                            OP50[0] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD734.0") * 100, 2);*/ Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 734, VarType.Real, 1).ToString()) * 100, 2);
                            OP60[0] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD738.0") * 100, 2);*/ Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 738, VarType.Real, 1).ToString()) * 100, 2);
                            OP70[0] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD742.0") * 100, 2);*/ Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 742, VarType.Real, 1).ToString()) * 100, 2);
                            OP80[0] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD746.0") * 100, 2);*/ Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 746, VarType.Real, 1).ToString()) * 100, 2);
                            OP90[0] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD750.0") * 100, 2);*/ Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 750, VarType.Real, 1).ToString()) * 100, 2);
                            OP100[0] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD754.0") * 100, 2);*/ Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 754, VarType.Real, 1).ToString()) * 100, 2);
                            //OP110[0] = Math.Round(PLC_Link[num].readReal("DB99.DBD930.0") * 100, 2);
                            //OP120[0] = Math.Round(PLC_Link[num].readReal("DB99.DBD952.0") * 100, 2);
                            YCok[0] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD770") * 100, 2);*/ Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 770, VarType.Real, 1).ToString()) * 100, 2);
                            #endregion

                            #region 数量
                            OP10[1] = PLC_Link[num].readInt("DB99.DBW290.0");
                            // OP10_2[1] = PLC_Link[num].readInt("DB99.DBW700.0");
                            OP20[1] = PLC_Link[num].readInt("DB99.DBW318.0");
                            OP30[1] = PLC_Link[num].readInt("DB99.DBW346.0");
                            OP40[1] = PLC_Link[num].readInt("DB99.DBW374.0");
                            OP50[1] = PLC_Link[num].readInt("DB99.DBW402.0");
                            OP60[1] = PLC_Link[num].readInt("DB99.DBW430.0");
                            OP70[1] = PLC_Link[num].readInt("DB99.DBW458.0");
                            OP80[1] = PLC_Link[num].readInt("DB99.DBW486.0");
                            OP90[1] = PLC_Link[num].readInt("DB99.DBW516.0");
                            OP100[1] = PLC_Link[num].readInt("DB99.DBW542.0");
                            // OP110[1] = PLC_Link[num].readInt("DB99.DBW920.0");
                            //完成数量
                            OP120[1] = PLC_Link[num].readInt("DB99.DBW542.0");
                            int h = Convert.ToInt32(DateTime.Now.ToString("HH"));   //h用于记录当天的时间
                            //for (int i = 0; i < 24; i = i + 2)
                            //{
                            //    if ((h >= i) && (h < i + 2))      //每两个小时取一次生产数
                            //    {
                            //        double sum = 0;
                            //        for (int j = 0; j < i / 2; j++)     //将之前的生产数加起来
                            //        {
                            //            sum += TimeS[j];
                            //        }
                            //        TimeS[i / 2] = OP120[1] - sum;      //计算出当前两小时的生产数
                            //    }
                            //}
                            for (int i = 8; i < 21; i++)    //早8~晚8
                            {
                                if ((h >= i) && (h < i + 1))      //每一个小时取一次生产数
                                {
                                    double sum = 0;
                                    for (int j = 8; j < i; j++)     //将之前的生产数加起来
                                    {
                                        sum += TimeS[j];
                                    }
                                    TimeS[i] = OP120[1] - sum;      //计算出当前一小时的生产数
                                }
                            }
                            for (int i = 20; i < 32; i++)    //晚8~早8
                            {
                                int k1;
                                int k2;
                                if (i < 24)
                                {
                                    k1 = i;
                                }
                                else
                                {
                                    k1 = i - 24;
                                }
                                if ((h >= k1) && (h < k1 + 1))      //每一个小时取一次生产数
                                {
                                    double sum = 0;
                                    for (int j = 20; j < i; j++)     //将之前的生产数加起来
                                    {
                                        if (j < 24)
                                        {
                                            k2 = j;
                                        }
                                        else
                                        {
                                            k2 = j - 24;
                                        }
                                        sum += TimeS[k2];
                                    }
                                    TimeS[k1] = OP120[1] - sum;      //计算出当前一小时的生产数
                                }
                            }
                            ////调试赋值
                            //if (!Clear)
                            //{
                            //    for (int i = 0; i < 12; i++)
                            //    {
                            //        TimeRangeValue[i] = i * 2;
                            //    }
                            //    Clear = true;
                            //}
                            //else
                            //{

                            //}
                            //白天与晚上生产数量
                            if (h >= 8 && h < 20)    //白班12小时
                            {
                                for (int i = 0; i < 12; i++)
                                {
                                    string a1 = (i + 8).ToString();
                                    string a2 = (i + 9).ToString();
                                    TimeRange[i] = a1 + "~" + a2;   //依次给白天生产计数表的X轴附上“8~9”..."19~20"
                                    TimeRangeValue[i] = TimeS[i + 8];   //依次给白天生产计数赋值
                                }
                            }
                            else   //剩下夜班12小时
                            {
                                for (int i = 0; i < 12; i++)
                                {
                                    int j = (i + 20) % 24;
                                    string a1 = j.ToString();
                                    string a2 = (j + 1).ToString();
                                    TimeRange[i] = a1 + "~" + a2;   //依次给晚上生产计数表的X轴附上“20~21”..."7~8"
                                    TimeRangeValue[i] = TimeS[j];   //依次给晚上生产计数赋值
                                }
                            }
                            //生产产量早8晚8清0
                            if ((h == 8 && !Clear) || (h == 20 && !Clear))
                            {
                                for (int i = 0; i < 24; i++)
                                {
                                    TimeS[i] = 0;
                                    if (i % 2 == 0)
                                    {
                                        TimeRangeValue[i / 2] = 0;
                                    }
                                }
                                Clear = true;
                            }
                            else if ((h == 19 && Clear) || (h == 7 && Clear))
                            {
                                Clear = false;
                            }
                            else
                            {

                            }
                            //计划数量
                            YCok[1] = PLC_Link[num].readInt("DB99.DBW288.0");
                            #endregion

                            #region 稼动率
                            //OP10_1[2] = Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 1099, 538, VarType.Real, 1).ToString()) * 100, 2);
                            OP10[2] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD774.0") * 100, 2);*/Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 774, VarType.Real, 1).ToString()) * 100, 2);
                            //OP10_2[2] = Math.Round(PLC_Link[num].readReal("DB99.DBD718.0") * 100, 2);
                            OP20[2] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD778.0") * 100, 2);*/Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 778, VarType.Real, 1).ToString()) * 100, 2);
                            OP30[2] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD782.0") * 100, 2);*/Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 782, VarType.Real, 1).ToString()) * 100, 2);
                            OP40[2] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD786.0") * 100, 2);*/Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 786, VarType.Real, 1).ToString()) * 100, 2);
                            OP50[2] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD790.0") * 100, 2);*/Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 790, VarType.Real, 1).ToString()) * 100, 2);
                            OP60[2] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD794.0") * 100, 2);*/Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 794, VarType.Real, 1).ToString()) * 100, 2);
                            OP70[2] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD798.0") * 100, 2);*/Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 798, VarType.Real, 1).ToString()) * 100, 2);
                            OP80[2] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD802.0") * 100, 2);*/Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 802, VarType.Real, 1).ToString()) * 100, 2);
                            OP90[2] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD806.0") * 100, 2);*/Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 806, VarType.Real, 1).ToString()) * 100, 2);
                            OP100[2] = /*Math.Round(PLC_Link[num].readReal("DB99.DBD810.0") * 100, 2);*/Math.Round(Convert.ToDouble(PLC_Link[num].plc.Read(DataType.DataBlock, 99, 810, VarType.Real, 1).ToString()) * 100, 2);
                            //OP110[2] = Math.Round(PLC_Link[num].readReal("DB99.DBD938.0") * 100, 2);
                            //稼动率
                            OP120[2] = Math.Round(PLC_Link[num].readReal("DB99.DBD960.0") * 100, 2);
                            //型号
                            YCok[2] = PLC_Link[num].ReadByte(DataType.DataBlock, 99, 535);
                            //YCok[2] = PLC_Link[num].readInt("DB99.DBW535.0");//OP100产品型号
                            #endregion

                            #region 节拍
                            OP10[3] = PLC_Link[num].readInt("DB99.DBW298.0");
                            //OP10_2[3] = PLC_Link[num].readInt("DB99.DBW702.0");
                            OP20[3] = PLC_Link[num].readInt("DB99.DBW326.0");
                            OP30[3] = PLC_Link[num].readInt("DB99.DBW354.0");
                            OP40[3] = PLC_Link[num].readInt("DB99.DBW382.0");
                            OP50[3] = PLC_Link[num].readInt("DB99.DBW410.0");
                            OP60[3] = PLC_Link[num].readInt("DB99.DBW438.0");
                            OP70[3] = PLC_Link[num].readInt("DB99.DBW466.0");
                            OP80[3] = PLC_Link[num].readInt("DB99.DBW494.0");
                            OP90[3] = PLC_Link[num].readInt("DB99.DBW522.0");
                            OP100[3] = PLC_Link[num].readInt("DB99.DBW550.0");
                            //OP110[3] = PLC_Link[num].readInt("DB99.DBW922.0");
                            //实际节拍
                            OP120[3] = PLC_Link[num].readInt("DB99.DBW276.0");
                            //预设节拍
                            YCok[3] = PLC_Link[num].readInt("DB99.DBW278.0");
                            #endregion
                            #region 呼叫
                            //if (_plcStruct.bool_0)
                            //{
                            //    if (callsys != 1)
                            //    {
                            //        callsys = 1;
                            //        sound = new SoundPlayer("op10呼叫班长.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }

                            //}
                            //else if (_plcStruct.bool_1)
                            //{
                            //    if (callsys != 2)
                            //    {
                            //        callsys = 2;
                            //        sound = new SoundPlayer("op10呼叫物料.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_2)
                            //{
                            //    if (callsys != 3)
                            //    {
                            //        callsys = 3;
                            //        sound = new SoundPlayer("op20呼叫班长.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_3)
                            //{
                            //    if (callsys != 4)
                            //    {
                            //        callsys = 4;
                            //        sound = new SoundPlayer("op20呼叫物料.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_4)
                            //{
                            //    if (callsys != 5)
                            //    {
                            //        callsys = 5;
                            //        sound = new SoundPlayer("op30呼叫班长.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_5)
                            //{
                            //    if (callsys != 6)
                            //    {
                            //        callsys = 6;
                            //        sound = new SoundPlayer("op30呼叫物料.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_6)
                            //{
                            //    if (callsys != 7)
                            //    {
                            //        callsys = 7;
                            //        sound = new SoundPlayer("op40呼叫班长.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_7)
                            //{
                            //    if (callsys != 8)
                            //    {
                            //        callsys = 8;
                            //        sound = new SoundPlayer("op40呼叫物料.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_8)
                            //{
                            //    if (callsys != 9)
                            //    {
                            //        callsys = 9;
                            //        sound = new SoundPlayer("op50呼叫班长.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_9)
                            //{
                            //    if (callsys != 10)
                            //    {
                            //        callsys = 10;
                            //        sound = new SoundPlayer("op50呼叫物料.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_10)
                            //{
                            //    if (callsys != 11)
                            //    {
                            //        callsys = 11;
                            //        sound = new SoundPlayer("op60呼叫班长.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_11)
                            //{
                            //    if (callsys != 12)
                            //    {
                            //        callsys = 12;
                            //        sound = new SoundPlayer("op60呼叫物料.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_12)
                            //{
                            //    if (callsys != 13)
                            //    {
                            //        callsys = 13;
                            //        sound = new SoundPlayer("op70呼叫班长.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_13)
                            //{
                            //    if (callsys != 14)
                            //    {
                            //        callsys = 14;
                            //        sound = new SoundPlayer("op70呼叫物料.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_14)
                            //{
                            //    if (callsys != 15)
                            //    {
                            //        callsys = 15;
                            //        sound = new SoundPlayer("op80呼叫班长.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_15)
                            //{
                            //    if (callsys != 16)
                            //    {
                            //        callsys = 16;
                            //        sound = new SoundPlayer("op80呼叫物料.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_16)
                            //{
                            //    if (callsys != 17)
                            //    {
                            //        callsys = 17;
                            //        sound = new SoundPlayer("op90呼叫班长.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_17)
                            //{
                            //    if (callsys != 18)
                            //    {
                            //        callsys = 18;
                            //        sound = new SoundPlayer("op90呼叫物料.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_18)
                            //{
                            //    if (callsys != 19)
                            //    {
                            //        callsys = 19;
                            //        sound = new SoundPlayer("op100呼叫班长.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_19)
                            //{
                            //    if (callsys != 20)
                            //    {
                            //        callsys = 20;
                            //        sound = new SoundPlayer("op100呼叫物料.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_20)
                            //{
                            //    if (callsys != 21)
                            //    {
                            //        callsys = 21;
                            //        sound = new SoundPlayer("op110呼叫班长.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_21)
                            //{
                            //    if (callsys != 22)
                            //    {
                            //        callsys = 22;
                            //        sound = new SoundPlayer("op110呼叫物料.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_22)
                            //{
                            //    if (callsys != 23)
                            //    {
                            //        callsys = 23;
                            //        sound = new SoundPlayer("op130呼叫班长.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else if (_plcStruct.bool_23)
                            //{
                            //    if (callsys != 24)
                            //    {
                            //        callsys = 24;
                            //        sound = new SoundPlayer("op130呼叫物料.wav");
                            //        sound.PlayLooping();
                            //        callover = false;
                            //    }
                            //}
                            //else
                            //{
                            //    if (callover == false)
                            //    {
                            //        sound.Stop();
                            //        sound.Dispose();
                            //        callover = true;
                            //    }
                            //}
                            //呼叫记录
                            #endregion



                            //数据记录
                            proData.operatorter = Program.userlogin.Admin;//操作员
                            //if (proData.save)
                            //{
                            //    //历史记录
                            //    string sql = "update productData set [OP130人工判定结果]='{0}',[OP130操作员]='{1}') where [内部码] = '{2}'";
                            //    sql = string.Format(sql, DateTime.Now, proData.typeName, proData.Ewm, proData.testStation, proData.allResult, proData.positivePressureValue, proData.negativePressureValue, proData.operatorter, proData.serialNumber);
                            //    int num1 = SQLHelp.AddData(sql);
                            //    //写入打印数据库
                            //    //string sql1 = "update print set  testDoneDate='{0}',serialNumber='{1}',printTemplate='{2}',LR='{3}'";
                            //    //sql1 = string.Format(sql1, DateTime.Now, proData.serialNumber, 1, proData.testStation);
                            //    //int num2 = SQLHelp.AddData(sql1);
                            //    proData.save = false;
                            //}
                        }
                    }
                    else
                    {
                        PLC_Link[num].plc.Close();
                        str = "ping失败";
                    }
                }
                catch (Exception ex1)
                {
                    str = "出现异常";
                    MessageBox.Show(Convert.ToString(ex1), "错误提示!");
                }

                if (str.Equals("成功"))
                {
                    label23.BackColor = System.Drawing.Color.Green;
                }
                else
                {
                    label23.BackColor = System.Drawing.Color.Red;
                }

                Thread.Sleep(300);

                label2.Text = "当前登录用户：{0} \t\t  使用公司：空调国际(上海)有限公司   \t 设备公司：重庆英之拓智能设备有限公司  \t  软件版本号：V1.0";
                label2.Text = string.Format(label2.Text, Program.userlogin.name);
            }
        }
        /// <summary>
        /// 权限验证
        /// </summary>
        /// <param name="userlogin"></param>
        /// <returns></returns>
        public static int PernChk(UserLogin userlogin)
        {
            userlogin = UserManagement.GetUser(userlogin.Admin, userlogin.Pwd);
            if (userlogin.Admin == null)
            {
                FormUserLoginInterface frmuserLogin = new FormUserLoginInterface();
                frmuserLogin.Text = "用户登陆";
                frmuserLogin.Icon = new Icon(@"UserLogin.ico");
                DialogResult Result = frmuserLogin.ShowDialog();
                if (Result == DialogResult.OK)
                {
                    return userlogin.UserID;
                }
                else
                {
                    return 4;
                }
            }
            else
            {
                return 4;
            }
        }

        private void 用户登录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            FormUserLoginInterface formUserLoginInterface = new FormUserLoginInterface();
            formUserLoginInterface.Text = "用户登陆";
            formUserLoginInterface.Icon = new Icon(@"UserLogin.ico");
            formUserLoginInterface.ShowDialog();
        }


        private void FormMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            //关闭主窗体时释放后台线程的资源
            for (int I = 0; I < PLC_Count; I++)
            {
                if (PLC_Td[I] != null)
                {
                    PLC_Td[I].Abort();
                }
            }

        }
        public static void inAlarm(string str)
        {
            string sql = "insert into Alarm([alarmTime],[alarmLable]) values('{0}','{1}')";
            sql = string.Format(sql, DateTime.Now, str);
            int num1 = SQLHelp.AddData(sql);
        }
        /// <summary>
        /// 等待客户端的连接 并且创建与之通信用的Socket
        /// </summary>
        /// 
        //将远程连接的客户端的IP地址和Socket存入集合中
        Dictionary<string, Socket> dicSocket = new Dictionary<string, Socket>();
        Socket socketSend;
        void Listen(object o)
        {
            Socket socketWatch = o as Socket;
            //等待客户端的连接 并且创建一个负责通信的Socket
            while (true)
            {
                try
                {
                    //负责跟客户端通信的Socket
                    socketSend = socketWatch.Accept();
                    //将远程连接的客户端的IP地址和Socket存入集合中
                    dicSocket.Add(socketSend.RemoteEndPoint.ToString(), socketSend);
                    //将远程连接的客户端的IP地址和端口号存储下拉框中
                    //cboUsers.Items.Add(socketSend.RemoteEndPoint.ToString());
                    //192.168.11.78：连接成功
                    //ShowMsg(socketSend.RemoteEndPoint.ToString() + ":" + "连接成功");
                    //开启 一个新线程不停的接受客户端发送过来的消息
                    //Thread th = new Thread(Recive);
                    //th.IsBackground = true;
                    //th.Start(socketSend);
                }
                catch
                { }
            }
        }
        /// <summary>
        /// 服务器给客户端发送消息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public void serverSend(string v, string IP)
        {
            string str = v;
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(str);
            List<byte> list = new List<byte>();
            list.Add(0);
            list.AddRange(buffer);
            //将泛型集合转换为数组
            byte[] newBuffer = list.ToArray();
            //buffer = list.ToArray();不可能
            //获得用户在下拉框中选中的IP地址
            string ip = IP;
            dicSocket[ip].Send(newBuffer);
            //     socketSend.Send(buffer);
        }

        private void 产品追溯ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.CloseForm();
            FormDataSelect formDataSelect = new FormDataSelect();
            this.OpenForm(formDataSelect);
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Image image = new Bitmap("公司LOGO.png");
            pictureBox1.Image = (Image)image;
        }

        private void 首页ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseForm();
        }

        private void 呼叫系统ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.CloseForm();
            FormDesplaySetting formDesplaySetting = new FormDesplaySetting();
            this.OpenForm(formDesplaySetting);
        }

        private void 统计ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseForm();
            OpenForm(new FormDataSelectGun());
        }

        private void 气检结果查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseForm();
            OpenForm(new FormDataSelectLeak());
        }

        private void 电检结果查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseForm();
            OpenForm(new FormDataSelectelEctricity());
        }

        private void 零件条码查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseForm();
            OpenForm(new FormDataSelectBarCode());
        }

        private void 电检蒸发箱结果查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseForm();
            OpenForm(new FormDataSelectelEctricity2());
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            //switch (callsys)
            //{
            //    case 1:
            //        SoundPlayer sound = new SoundPlayer("op10呼叫班长.wav");
            //        sound.Play();
            //        break;

            //}

        }

        private void 呼叫记录ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseForm();
            OpenForm(new FormDataSelectCall());
        }

        private void 螺钉扭力查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseForm();
            OpenForm(new FormDataSelectNiuli());
        }

        private void 返修查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            CloseForm();
            OpenForm(new FormDataSelectRepair());
        }

        private void 百度电检数据查询ToolStripMenuItem_Click(object sender, EventArgs e)    //点击百度电检数据查询打开第二个电检查询界面
        {
            CloseForm();
            OpenForm(new FormDataSelectelEctricity2());
        }
    }
}
